package utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.List;

/**
 * @version V1.0
 * @Title:
 * @Package
 * @Description:
 * @author: GaleBlade
 * @date:
 */
public class RedisUtils {

    private JedisPool pool;
    public Jedis jedis;

    public RedisUtils(String ip,String port,String auth){
        pool = new JedisPool(ip,Integer.parseInt(port));
        //从连接池中获得Jedis对象
        jedis = pool.getResource();
        // 如果 Redis 服务设置了密码，需要下面这行，没有就不需要
        if (!auth.equals("")){
            jedis.auth(auth);
        }
    }

    public void close(){
        jedis.close();
        pool.close();
    }

    public void select(int db){
        jedis.select(db);
    }

    public String getValue(String key, String field){
        List<String> cacheList = jedis.hmget(key,field);
        return cacheList.get(0);
    }

    public long setValue(String key, String field, String value){
        return jedis.hset(key, field, value);
    }

}
